//
//  HWGameCommunityListFooter.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/19.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameCommunityListFooter: UICollectionReusableView {
    
    var bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12*widthScale, corners: [.layerMinXMaxYCorner,.layerMaxXMaxYCorner]).end()
    var shareImage:UIImageView!
    var shareLabel:UILabel!
    var shareButton:UIButton!
    var commentImage:UIImageView!
    var commentLabel:UILabel!
    var commentButton:UIButton!
    var zanImage:UIImageView!
    var zanLabel:UILabel!
    var zanButton:UIButton!
    var delButton:UIButton!
//    var lineView:UIView!
    var detailButton:UIButton!
    var jumpToDetail:((_:HWGameCommunityListModel)->())?
    var zanButtonClick:((_:HWGameCommunityListModel)->())?
    var delButtonClick:((_:HWGameCommunityListModel)->())?
    var model:HWGameCommunityListModel? {
        didSet {
            zanLabel.text = model?.good
            commentLabel.text = model?.pinglun
            shareLabel.text = model?.look
            if model?.is_pinglun == true {
                commentImage.image = UIImage(named: "communityCommentSelect")
                commentLabel.textColor = .colorWithHexString(hexString: "#FA8C08")
            } else {
                commentImage.image = UIImage(named: "communityCommentNormal")
                commentLabel.textColor = .colorWithHexString(hexString: "#888888")
            }
            
            if model?.is_good == true {
                zanImage.image = UIImage(named: "communityZanSelect")
                zanLabel.textColor = .colorWithHexString(hexString: "#FA8C08")
            } else {
                zanImage.image = UIImage(named: "communityZanNormal")
                zanLabel.textColor = .colorWithHexString(hexString: "#888888")
            }
            
            if model?.del == true {
                delButton.isHidden = false
            } else {
                delButton.isHidden = true
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCommunityListFooter {
    private func initUI() -> Void {
        
        addSubview(bgView)
        
        detailButton = UIButton(type: .custom)
        detailButton.addTarget(self, action: #selector(detailButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(detailButton)
        
        shareImage = UIImageView()
        shareImage.image = UIImage(named: "chakankaiyang")
        bgView.addSubview(shareImage)
        
        shareLabel = HWLabel.createLabel(title: "", color: "#888888", font: 13*widthScale)
        bgView.addSubview(shareLabel)
        
        shareButton = UIButton(type: .custom)
        shareButton.addTarget(self, action: #selector(shareButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(shareButton)
        
        commentImage = UIImageView()
        commentImage.image = UIImage(named: "communityCommentNormal")
        bgView.addSubview(commentImage)
        
        commentLabel = HWLabel.createLabel(title: "0", color: "#888888", font: 13*widthScale)
        bgView.addSubview(commentLabel)
        
        commentButton = UIButton(type: .custom)
        commentButton.addTarget(self, action: #selector(commentButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(commentButton)
        
        zanImage = UIImageView()
        zanImage.image = UIImage(named: "communityZanNormal")
        bgView.addSubview(zanImage)
        
        zanLabel = HWLabel.createLabel(title: "0", color: "#888888", font: 13*widthScale)
        bgView.addSubview(zanLabel)
        
        zanButton = UIButton(type: .custom)
        zanButton.addTarget(self, action: #selector(zanButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(zanButton)
        
        delButton = UIButton(type: .custom)
        delButton.setImage(UIImage(named: "communityDel"), for: .normal)
        delButton.addTarget(self, action: #selector(delButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(delButton)
        
//        lineView = UIView()
//        lineView.backgroundColor = .colorWithHexString(hexString: "#EEEEEE")
//        bgView.addSubview(lineView)
    }
    
    private func initFrame() -> Void {
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(12*widthScale)
            make.right.bottom.equalTo(-12*widthScale)
            make.top.equalToSuperview()
        }
        
        detailButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        shareImage.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(20*widthScale)
            make.width.height.equalTo(14*widthScale)
        }
        
        shareLabel.snp.makeConstraints { make in
            make.left.equalTo(shareImage.snp.right).offset(4*widthScale)
            make.height.equalTo(18*widthScale)
            make.centerY.equalTo(shareImage)
        }
        
        shareButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(shareImage)
            make.right.equalTo(shareLabel)
        }
        
        commentImage.snp.makeConstraints { make in
            make.left.equalTo(shareLabel.snp.right).offset(36*widthScale)
            make.top.equalTo(20*widthScale)
            make.width.height.equalTo(14*widthScale)
        }
        
        commentLabel.snp.makeConstraints { make in
            make.left.equalTo(commentImage.snp.right).offset(4*widthScale)
            make.height.equalTo(18*widthScale)
            make.centerY.equalTo(commentImage)
        }
        
        commentButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(commentImage)
            make.right.equalTo(commentLabel)
        }
        
        zanImage.snp.makeConstraints { make in
            make.left.equalTo(commentLabel.snp.right).offset(36*widthScale)
            make.top.equalTo(20*widthScale)
            make.width.height.equalTo(14*widthScale)
        }
        
        zanLabel.snp.makeConstraints { make in
            make.left.equalTo(zanImage.snp.right).offset(4*widthScale)
            make.height.equalTo(18*widthScale)
            make.centerY.equalTo(zanImage)
        }
        
        zanButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(zanImage)
            make.right.equalTo(zanLabel)
        }
        
        delButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.width.height.equalTo(14*widthScale)
            make.centerY.equalTo(shareImage)
        }
        
//        lineView.snp.makeConstraints { make in
//            make.bottom.equalTo(-1*widthScale)
//            make.height.equalTo(1*widthScale)
//            make.left.equalTo(18*widthScale)
//            make.right.equalTo(-18*widthScale)
//        }
    }
}

extension HWGameCommunityListFooter {
    
    @objc func detailButtonClick(_:UIButton) -> Void {
        jumpToDetail?(self.model ?? HWGameCommunityListModel())
    }
    
    @objc func shareButtonClick(_:UIButton) -> Void {
        jumpToDetail?(self.model ?? HWGameCommunityListModel())
    }
    
    @objc func commentButtonClick(_:UIButton) -> Void {
        jumpToDetail?(self.model ?? HWGameCommunityListModel())
    }
    
    @objc func zanButtonClick(_:UIButton) -> Void {
        self.zanButtonClick?(self.model ?? HWGameCommunityListModel())
    }
    
    @objc func delButtonClick(_:UIButton) -> Void {
        self.delButtonClick?(self.model ?? HWGameCommunityListModel())
    }
}
